﻿using ArtMath.Attribute;
using ArtMath.Component.CoordinateSystem;
using ArtMath.Core.Data;
using ArtMath.Core.Helper;
using ArtMath.Data;
using ArtMath.Helper;
using ArtMath.Setting;
using System;
using System.ComponentModel;
using System.Drawing;
using ArtMath.CommonType;

namespace ArtMath.Component.Annotation
{
    /// <summary>
    /// 文本框
    /// </summary>
    [Serializable]
    public class TextArea : CustomBase
    {
        PlainText pt;
        protected ColorX textColor, bgColor;
        public TextArea(SketchPad pad, string name, string text, bool addToList = true) : base(pad, name, ToolTypeName.wb, addToList)
        {
            textColor = new ColorX(Pad, Settings.Instance.TextColor);
            bgColor = new ColorX(Pad, Settings.Instance.TextBackgroundColor);
            pt = new PlainText(text)
            {
                Font = Settings.Instance.TextFont,
                Alignment = Settings.Instance.TextAlignment
            };
        }
        [Browsable(false)]
        public PlainText InnerText
        {
            get { return pt; }
        }

        [MultilingualCategory("Structure"), MultilingualDisplayName("Text")]
        public virtual string Text
        {
            get { return InnerText.Text; }
            set { InnerText.Text = value; }
        }
        [MultilingualCategory("Appearance"), MultilingualDisplayName(null, "Text", "Font")]
        public Font TextFont
        {
            get { return InnerText.Font; }
            set { InnerText.Font = value; }
        }
        [MultilingualCategory("Appearance"), MultilingualDisplayName(null, "Text", "Color"), DefaultValue(typeof(Color), "Black")]
        public Color TextColor
        {
            get { return textColor.Color; }
            set { textColor.Color = value; }
        }
        [MultilingualCategory("Appearance"), MultilingualDisplayName(null, "Background", "Color"), DefaultValue(typeof(Color), "White")]
        public Color BackgroundColor
        {
            get { return bgColor.Color; }
            set { bgColor.Color = value; }
        }
        [MultilingualCategory("Appearance"), MultilingualDisplayName(null, "Text", "Alignment"), DefaultValue(typeof(ContentAlignment), "TopLeft")]
        public ContentAlignment TextAlignment
        {
            get { return InnerText.Alignment; }
            set { InnerText.Alignment = value; }
        }
        public override void CreateComponents()
        {
            Location.SetFromPointD(Pad.PxyToMath(SpareCoordinate_));
            if (!FitSize)
            {
                Size.SetFromSizeD(SizeD.FromSize(InnerText.Size));
            }
            base.CreateComponents();
        }
        public override SizeF ActualSize
        {
            get
            {
                if (!FitSize)
                {
                    return InnerText.Size;
                }
                return base.ActualSize;
            }
        }
        public override object GetCopyInfo()
        {
            return Tuple.Create(Location.ToPointD());
        }
        public override void AutoCreate(object createInfo, bool tryOffset = false)
        {
            Tuple<PointD> info = createInfo as Tuple<PointD>;
            if (info == null) return;
            PointD[] tmp = new PointD[] { info.Item1 };
            if (tryOffset)
            {
                Pad.CopyOffset(ToolType, tmp);
            }
            PolarCoordinate pc = Pad.Coordinate as PolarCoordinate;
            Location.SetFromPointD(pc != null ? pc.XYtoRT(tmp[0]) : tmp[0]);
            base.CreateComponents();
        }
        public override object CopyAndPaste(SketchPad pad)
        {
            TextArea ta = new TextArea(pad, Utility.GetNextAvailableName(Name, pad.CheckObjectNameExist), Text, LayerItem != null);
            afterNewCopy(ta);
            return ta;
        }
        public override void Paint(Graphics gra)
        {
            if (ObjectState == ObjectState.Normal && Visible)
            {
                gra.Transform(Pxy, Settings.Instance.SelfRotate ? -RotateAngle.DegreeValue : -selfRotateAngle * Core.Helper.Angle.RToD, () =>
                {
                    RectangleF rect = new RectangleF(new PointF(0, 0), ActualSize);
                    gra.FillRectangle(new SolidBrush(StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? bgColor.ReversedColor : bgColor.Color), rect);
                    InnerText.Color = StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? textColor.ReversedColor : textColor.Color;
                    InnerText.Paint(gra, rect);
                });
            }
            base.Paint(gra);
        }
    }
}
